//
//  ToolObjc.m
//  hellomui
//
//  Created by zhangtao on 15/10/30.
//  Copyright © 2015年 zt.td. All rights reserved.
//

#import "ToolObjc.h"

@implementation ToolObjc


+(NSMutableDictionary *)md5:(NSMutableDictionary *)dic{
    
    
    
    NSArray *dics = [dic allKeys];
    NSArray *sortDics = [dics sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        return [obj1 compare:obj2 options:NSNumericSearch];
    }];
    
    NSString *appendStr;// = @"";
    for (NSString *str in sortDics) {
        
        NSString *key = [NSString stringWithFormat:@"%@=",str];
        NSString *value = [dic valueForKey:str];
        
        if (!appendStr) {
            appendStr = [key stringByAppendingString:value];
        }else{
            appendStr = [[appendStr stringByAppendingString:key] stringByAppendingString:value];
        }
        
        
    }

    
    const char *cStr =[[appendStr stringByAppendingString:@"study100" ] UTF8String];
    NSLog(@"lllllll:%s",cStr);
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr,(CC_LONG)strlen(cStr), result); // This is the md5 call
    
    
    
    
    NSString *md5 = [NSString stringWithFormat:
             @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
             result[0], result[1], result[2], result[3],
             result[4], result[5], result[6], result[7],
             result[8], result[9], result[10], result[11],
             result[12], result[13], result[14], result[15]
             ];

    
    [dic setValue:md5 forKey:@"bd_sig"];
    return dic;
    
}


+(NSString *)range:(NSString *)str{
    NSString *searchText = @"rangeOfString";
    NSRange range = [searchText rangeOfString:@"ga" options:NSRegularExpressionSearch];
    if (range.location != NSNotFound) {
        NSLog(@"range ：%@", [searchText substringWithRange:range]);
    }
    
    return nil;
}



+(NSString *)md5Str:(NSDictionary *)dic{
    NSArray *dics = [dic allKeys];
    NSArray *sortDics = [dics sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        
        return [obj1 compare:obj2 options:NSNumericSearch];
    }];
    
    NSString *appendStr;// = @"";
    for (NSString *str in sortDics) {
        
        NSString *key = [NSString stringWithFormat:@"%@=",str];
        NSString *value = [dic valueForKey:str];
        
        if (!appendStr) {
            appendStr = [key stringByAppendingString:value];
        }else{
            appendStr = [[appendStr stringByAppendingString:key] stringByAppendingString:value];
        }
    }
    
    const char *cStr =[[appendStr stringByAppendingString:@"study100" ] UTF8String];
    NSLog(@"lllllll:%s",cStr);
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr,(CC_LONG)strlen(cStr), result); // This is the md5 call
    
    
    NSString *md5 = [NSString stringWithFormat:
                     @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                     result[0], result[1], result[2], result[3],
                     result[4], result[5], result[6], result[7],
                     result[8], result[9], result[10], result[11],
                     result[12], result[13], result[14], result[15]
                     ];
    
    
    return md5;

}


+(void)showMessage:(NSString *)message
{
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    UIView *showview =  [[UIView alloc]init];
    showview.backgroundColor = [UIColor blackColor];
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.alpha = 1.0f;
    showview.layer.cornerRadius = 5.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc]init];
    CGSize LabelSize = [message sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(290, 9000)];
    label.frame = CGRectMake(10, 5, LabelSize.width, LabelSize.height);
    label.text = message;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:15];
    [showview addSubview:label];
    showview.frame = CGRectMake((MAIN_WIDTH - LabelSize.width - 20)/2,200, LabelSize.width+20, LabelSize.height+10);
    [UIView animateWithDuration:1.5 animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
    }];
}


+(void)shoVersionMessage:(NSString*)message
{

    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    UIView *showview =  [[UIView alloc]init];
    showview.backgroundColor = [UIColor colorWithRed:77/255.0 green:77/255.0 blue:77/255.0 alpha:1];
//    showview.alpha=0.5;
    showview.frame = CGRectMake(1, 1, 1, 1);
    showview.alpha = 1.0f;
    showview.layer.cornerRadius = 5.0f;
    showview.layer.masksToBounds = YES;
    [window addSubview:showview];
    
    UILabel *label = [[UILabel alloc]init];
    CGSize LabelSize = [message sizeWithFont:[UIFont systemFontOfSize:22] constrainedToSize:CGSizeMake(290, 9000)];
    label.frame = CGRectMake(25, 10, LabelSize.width, LabelSize.height);
    label.text = message;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = 1;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:17];
    [showview addSubview:label];
    showview.frame = CGRectMake((MAIN_WIDTH - LabelSize.width)/2 - 25,150, LabelSize.width+50, LabelSize.height+20);
    showview.center=window.center;
    [UIView animateWithDuration:1.5 animations:^{
        showview.alpha = 0;
    } completion:^(BOOL finished) {
        [showview removeFromSuperview];
    }];

}

+(void)thredLogin:(NSString *)type{
    NSString *path = @"login/threeQQWX";
    
    NSString *openId = [NSString stringWithFormat:@"%@",NSUSER_DEF(@"OPENID")];
    NSDictionary *dic = @{@"openId":openId,@"type":type};
    
    NSDictionary *dics = @{@"openId":openId,@"type":type,@"bd_sig":[self md5Str:dic]};
    [[HttpConnctionManager sharedInstance]requestParam:dics andUrl:path andSuccessedBlock:^(NSDictionary *succeedResult) {
        NSLog(@"ABCDEFG~~~~~%@",[succeedResult valueForKey:@"resData"]);
        
        NSString *recode =[NSString stringWithFormat:@"%@", [succeedResult valueForKey:@"resCode"]];
        
        if ([recode isEqualToString:@"0"]) {   //login-->>success
            
            NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]valueForKey:@"userId"], @"USERID");
            
            NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"address"], @"ADDRESS");
            
            NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"email"], @"EMAIL");
            
            NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"gener"], @"GENER");
            
            NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"headImg"], @"HEADIMG");
            
            NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"nickName"], @"NICKNAME");
            
            NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"operatorId"], @"OPERATORID");
            
            NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"phone"], @"PHONE");
            
            NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"state"], @"STATE");
            
            
            NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"trueName"], @"TRUENAME");
            
            
            NSUSER_DEF_NORSET([[succeedResult valueForKey:@"resData"]stringWithFilted:@"userName"], @"USERNAME");
        }
        
    } andFailedBlock:^(AFHTTPRequestOperation *response, NSError *error) {
        
    }];
    
    
}


+(void)upDataImage:(NSData *)imageData{
    NSString *path = @"login/uploadUserHeadImg";
    
    NSString *userId = [NSString stringWithFormat:@"%@",NSUSER_DEF(@"USERID")];
    NSDictionary *dic = @{@"userId":userId};
    NSDictionary *dicVal =@{@"userId":userId,@"headImg":imageData,@"bd_sig":dic};
    [[HttpConnctionManager sharedInstance]startMulitDataPost:path postFile:imageData paragram:dicVal successedBlock:^(NSDictionary *succeedResult) {
        NSLog(@"111111abc%@",[succeedResult valueForKey:@"resMessage"]);
    } failedBolck:^(AFHTTPRequestOperation *response, NSError *error) {
        NSLog(@"22222");
    }];
    
}


+(void)loginOUtLoginIn:(NSString *)userid{
//    NSString *path = @"secondClass/sessionStorage";
    NSDictionary *dics = @{@"userId":userid};
    NSDictionary *dic  = @{@"userId":userid,@"bd_sig":[self md5Str:dics]};

    NSString *path = @"http://182.92.117.144:8080/study-manager/secondClass/sessionStorage";
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
//    [manager.requestSerializer setValue:@"text/html" forHTTPHeaderField:@"content-Type"];
    //    AFURLResponseSerializationErrorDomain
    [manager GET:path parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
         NSLog(@"后台记录成功%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"后台没记录");

    }];
    
}

+(void)loginOut{
//    NSString *status;
    NSString *path = @"http://182.92.117.144:8080/study-manager/secondClass/sessionStorage";
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager setRequestSerializer:[AFHTTPRequestSerializer serializer]];
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
    //    [manager.requestSerializer setValue:@"text/html" forHTTPHeaderField:@"content-Type"];
    //    AFURLResponseSerializationErrorDomain
    [manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
   
       
            [self showMessage:@"退出成功"];
              NSLog(@"后台记录成功%@",responseObject);
    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"后台没记录");
        
    }];
    
    
}


@end
